package com.demo.controller;


import com.demo.entity.EasyUIResult;
import com.demo.entity.User;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("user")
@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	
	/*@RequestMapping(value="users")
	public String toUsers(){
		
		return "users";
	}
	
	@RequestMapping(value="page/add")
	public String toUserAdd(){
		return "user-add";
	}*/
	
	@RequestMapping(value="page/{pageName}")
	public String toPage(@PathVariable("pageName")String pageName){
		return pageName;
	}
	
	@RequestMapping(value="list")
	@ResponseBody
	public EasyUIResult queryUserListByPage(@RequestParam("page")Integer pageNum, @RequestParam("rows")Integer pageSize){
		return this.userService.queryUserListByPage(pageNum, pageSize);
	}
	
	@RequestMapping(value="save")
	@ResponseBody
	public Map<String, Object> insertUser(User user){
		Map<String, Object> map = new HashMap<>();
		Boolean b = this.userService.insertUser(user);
		if(b){
			map.put("status", 200);
		} else {
			map.put("status", 500);
		}
		return map;
	}

	@RequestMapping(value = "login",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String login(@RequestParam("username")String name,@RequestParam("password")String password){
		Boolean flag = userService.login(name,password);
		if(flag){
			return "success";
		}else{
			return "login";
		}
	}
}
